import { request } from '@/service/request';

/**
 * 商品服务
 */
export class GoodsService {
    /**
     * 获取商品树形子级数据
     */
    static async loadTreeChildrenNew(params: { pid: string }) {
        try {
            const response = await request.get<any>('/nurse_goods/nurseGoods/loadTreeChildrenNew', params, {
                useErrMsg: true
            });
            
            if (response.error) {
                // console.error('获取商品树形数据失败:', response.error);
                return { error: response.error, success: null };
            }
            
            return { error: null, success: response.success };
        } catch (error) {
            // console.error('获取商品树形数据失败:', error);
            return { error, success: null };
        }
    }

    /**
     * 获取商品列表
     */
    static async getGoodsList(params: { pid?: string; nurse_type?: string; hospital?: string }) {
        try {
            const response = await request.get<any>('/nurse_goods/nurseGoods/listByWeChat', params, {
                useErrMsg: true
            });
            
            if (response.error) {
                // console.error('获取商品列表失败:', response.error);
                return { error: response.error, success: null };
            }
            
            return { error: null, success: response.success };
        } catch (error) {
            // console.error('获取商品列表失败:', error);
            return { error, success: null };
        }
    }
}

/**
 * 商品 Hook
 */
export function useNurseGoods() {
    const loadTreeChildrenNew = async (params: { pid: string }) => {
        return await GoodsService.loadTreeChildrenNew(params);
    };

    const getGoodsList = async (params: { pid?: string; nurse_type?: string; hospital?: string }) => {
        return await GoodsService.getGoodsList(params);
    };

    return {
        loadTreeChildrenNew,
        getGoodsList
    };
}
